<?php
namespace App\Service\Classes;
use App\Service\Model\Cards;
use App\Service\Model\ApiLog;

class Pay
{
    private $api;
    private $basic;
    private $orgid;
    private $key;
    private $appid;
    private $randomstr;
    private $cusid;
    private $notify_url;
    public  $retmsg;

    const ACCTTP = '01'; //卡类型. 00-借记卡  01-存折

    public function __construct()
    {

        loadLibrary('curl.class');
        loadLibrary('common');

        $this->api        = config("pay.api");
        $this->basic      = config('pay.url');
        $this->appid      = config('pay.appid');
        $this->key        = config('pay.key');
        $this->orgid      = config('pay.orgid');
        $this->notify_url = config('pay.notify_url');
        $this->randomstr  = randomNum();
    }

    /**
     * call api command
     * @param string $method get|post
     * @param string $command
     * @param array $params
     * @return false|object
     */
    protected function command($method, $command, $params = [])
    {
        $curl = new \Curl();
        $curl->setSsl();
        $curl->setTimeout(5, 30);
        $params['appid']     = $this->appid;
        $params['randomstr'] = $this->randomstr;
        $params['orgid']     = $this->orgid;
        $params['key']       = $this->key;
        ksort($params);
        $params['sign']      =  md5($this->build_params($params));
        $params              =  array_except($params, 'key');
        $url = $this->basic.$this->api[$command];
        $data = call_user_func([$curl, $method], $url, $params);
        $statusCode = $curl->getHttpCode();
        $curl->close();
        $result = json_decode($data);
        apiLog($this->api[$command] , $statusCode , $params , $result);
        return $result;
    }

    /**
     * 绑定信用卡
     *
     * @param [type] $cardno
     * @param [type] $acctname
     * @param [type] $validdate
     * @param [type] $cvv2
     * @param [type] $tel
     * @param [type] $idno
     * @return void
     */
    public function bind($cusid , $cardno , $acctname , $validdate , $cvv2 , $tel , $idno )
    {
        return $this->command('POST', 'pay.bindcard', [
            'cusid'      => $cusid,
            'meruserid'  => user()->getKey(),
            'cardno'     => $cardno,
            'acctname'   => $acctname,
            'accttype'   => Cards::credit_card,
            'validdate'  => $validdate,
            'cvv2'       => $cvv2,  
            'tel'        => $tel,
            'idno'       => $idno,
        ]);
    }
    /**
     * 绑卡确认
     *
     * @param string $cardno
     * @param string $acctname
     * @param string $validdate
     * @param string $cvv2
     * @param string $tel
     * @param string $idno
     * @param string $smscode
     * @param string $thpinfo
     * @return void
     */
    public function confirm($cardno , $acctname , $validdate , $cvv2 , $tel , $idno , $smscode ,$cusid ,$uid){
        return $this->command('POST', 'pay.confirm', [
            'cusid'      => $cusid,
            'meruserid'  => $uid,
            'cardno'     => $cardno,
            'acctname'   => $acctname,
            'accttype'   => Cards::credit_card,
            'validdate'  => $validdate,
            'cvv2'       => $cvv2,
            'tel'        => $tel,
            'idno'       => $idno,
            'smscode'    => $smscode,
        ]);
    }

    /**
     * 进件
     *
     * @param string $acctname
     * @param string $idno
     * @return array|boolean
     */
    public function addcus( $outcusid ,  $acctname , $idno , $tel ,$cardno ,$province ,$city , $address)
    {
        $params     =   [
            'belongorgid'   => $this->orgid,
            'outcusid'      => $outcusid,
            'cusname'       => $acctname,
            'cusshortname'  => $acctname,
            'merprovice'    => $province,
            'areacode'      => $city,
            'legal'         => $acctname,
            'idno'          => $idno,
            'phone'         => $tel,
            'address'       => $address,
            'acctid'        => $cardno,
            'acctname'      => $acctname,
            'accttp'        => self::ACCTTP,
            'prodlist'      => "[{'trxcode':'QUICKPAY_NOSMS','feerate':'0.80'},{'trxcode':'TRX_PAY','feerate':'1'}]",
        ];
        return $this->command('POST', 'pay.addcus', $params);
    }

    /**
     * 解绑银行卡
     *
     * @param [type] $cardno
     * @return void
     */
    public function unbind($cusid , $cardno){
        return $this->command('POST', 'pay.unbind',[
            'cusid'      => $cusid,
            'cardno'     => $cardno,
        ]);
    }


    /**
     * 重新发送验证码
     *
     * @param [type] $cardno
     * @param [type] $acctname
     * @param [type] $validdate
     * @param [type] $cvv2
     * @param [type] $tel
     * @param [type] $idno
     * @param [type] $thpinfo
     * @return void
     */
    public function resendSmsCode($cardno , $acctname , $validdate , $cvv2 , $tel , $idno ,$cusid , $uid ){

        return $this->command('POST', 'pay.resend', [
            'cusid'      => $cusid,
            'meruserid'  => $uid,
            'cardno'     => $cardno,
            'acctname'   => $acctname,
            'accttype'   => Cards::credit_card,
            'validdate'  => $validdate,
            'cvv2'       => $cvv2,
            'tel'        => $tel,
            'idno'       => $idno,
        ]);
    }



    /**
     * 快捷支付订单
     *
     * @param string $agreeid
     * @param int $amount
     * @param double $fee
     * @return void
     */
    public function quickpass($orderId  ,$agreeid , $amount , $cusid ){
        return $this->command('POST', 'pay.quickpass', [
            'cusid'      => $cusid,
            'orderid'    => $orderId,
            'agreeid'    => $agreeid,
            'amount'     => $amount * 100,
            //'fee'        => $fee,
            'currency'   => 'CNY',
            'subject'    => '快捷小额免短信支付:'.$orderId,
            'notifyurl'  => $this->notify_url,
        ]);
    }




    /**
     * 查询当前用户的余额
     *
     * @param string $cusid
     * @return object
     */
    public function queryBalance($cusid){
        return $this->command('POST', 'pay.balance', [
            'cusid'      => $cusid,
        ]);
    }

    /**
     * 提现
     *
     * @param [type] $cusid
     * @param [type] $amount
     * @return void
     */
    public function withdraw($cusid , $amount , $orderId ,$isAll = 0){
        return $this->command('POST', 'pay.withdraw', [
            'cusid'      => $cusid,
            'amount'     => $amount * 100,
            'orderid'    => $orderId,
            'notifyurl'  => $this->notify_url,
            'isall'      => $isAll,
        ]);
    }
    
    /**
     * 付款
     *
     * @param [type] $cusid
     * @param [type] $amount
     * @param [type] $orderId
     * @param [type] $agreeid
     * @param integer $isAll
     * @return void
     */
    public function acctpay($cusid, $amount ,$orderId , $agreeid , $isall = 0 ,$fee){
        return $this->command('POST', 'pay.acctpay', [
            'cusid'      => $cusid,
            'amount'     => $amount * 100,
            'orderid'    => $orderId,
            'notifyurl'  => $this->notify_url,
            'fee'        => $fee * 100,
            'agreeid'    => $agreeid,
            'isall'      => $isall,
        ]);
    }

    /**
     * 修改费率
     *
     * @param [type] $cusid
     * @param [type] $prodlist
     * @return void
     */
    public function updatesettinfo($cusid,$prodlist){
        return $this->command('POST','pay.updatesettinfo',[
            'cusid'     => $cusid,
            'prodlist'  => $prodlist,
        ]);
    }
    /**
     * Undocumented function
     *
     * @param [type] $result
     * @return void
     */
    public function check($result)
    {
        if (is_object($result)) {
            if (isset($result->retcode) && $result->retcode == 'FAIL') {
                return false;
            }
        }
        return true;
    }
    /**
     * Undocumented function
     *
     * @param [type] $params
     * @return void
     */
    protected function build_params($params)
    {
        $str  = '';
        foreach ($params as $k => $v) {
            $str .= $k.'='.$v.'&';
        }
        $str  = rtrim($str, "&");
        return $str;
    }

    /**
     * 生成在线支付订单号
     * @return string
     */
    public function generate_orderId(){
        $date = Carbon::now()->formatLocalized("%Y%m%d%H%M%S");
        $rand = rand(1000 ,9999);
        return $date . $rand;
    }
}
